const path = require("path")
const CleanWebpackPlugin = require("clean-webpack-plugin")
const ExtractTextPlugin = require("extract-text-webpack-plugin")
const CopyWebpackPlugin = require("copy-webpack-plugin")
const entry = require("../entry.js")

const extractLess = new ExtractTextPlugin({
  filename: "[name].css",
  disable: process.env.NODE_ENV === "development"
})

module.exports = {
  entry: entry.createEntry(),
  output: {
    filename: "[name].js",
    path: path.resolve(__dirname, "..", "dist"),
    publicPath: "/"
  },
  externals: {
    jquery: "jQuery",
    $: "jQuery"
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        include: __dirname + 'src',
        use: {
          loader: "babel-loader",
          options: {
            presets: ["env"],
            plugins: ["@babel/transform-runtime"]
          }
        }
      },
      {
        test: /\.less$/,
        exclude: /node_modules/,
        use: extractLess.extract({
          use: [
            {
              loader: "css-loader"
            },
            {
              loader: "less-loader"
            }
          ],
          fallback: "style-loader"
        })
      }
      // {
      //   test: /\.(html)$/,
      //   use: {
      //     loader: "html-loader",
      //     options: {
      //       attrs: [":data-src"]
      //     }
      //   }
      // }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(),
    new CopyWebpackPlugin([
      {
        from: path.resolve(__dirname, "..", "src/assets/"),
        to: path.resolve(__dirname, "..", "dist/assets"),
        toType: 'dir'
      }
    ]),
    extractLess,
    ...entry.createHtmlTemplate()
  ]
}

